package com.homedo.odin.solution.service.bizservice.wap;

import com.homedo.core.common.base.dto.Meta;
import com.homedo.core.common.exception.BizException;
import com.homedo.core.common.util.NumberHelper;
import com.homedo.odin.solution.contract.wap.UserLoginAddReq;
import com.homedo.odin.solution.contract.wap.UserLoginResp;
import com.homedo.odin.solution.model.entity.UserLogin;
import com.homedo.odin.solution.service.UserLoginServiceImpl;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author: donglibing
 * @date: 2018/3/27 17:36
 * @Description:
 */
@Service
public class UserLoginBizService {

    /**
     * 增加用户登录信息
     *
     * @param userLoginAddRequest
     * @return
     */
    @Autowired
    private UserLoginServiceImpl userLoginService;

    /**
     * 增加用户登陆信息
     */
    public void addUserLogin(UserLoginAddReq userLoginAddRequest) {
        if (userLoginAddRequest == null ||
                !NumberHelper.validatePositive(userLoginAddRequest.getUserId()) ||
                StringUtils.isEmpty(userLoginAddRequest.getOpenId()) ||
                StringUtils.isEmpty(userLoginAddRequest.getBizType()) ||
                StringUtils.isEmpty(userLoginAddRequest.getLoginType())) {
            throw new BizException(Meta.CODE_451.getCode(), Meta.CODE_451.getMsg());
        }
        if (!ArrayUtils.contains(new String[]{"wx", "qq", "ali"}, userLoginAddRequest.getLoginType())) {
            throw new BizException(Meta.CODE_451.getCode(), "登陆类型信息非法！");
        }
        if (!ArrayUtils.contains(new String[]{"all", "sol", "wy"}, userLoginAddRequest.getBizType())) {
            throw new BizException(Meta.CODE_451.getCode(), "业务归属 信息非法！");
        }
        UserLogin userLogin = userLoginService.getUserLogin(userLoginAddRequest.getOpenId());
        if (userLogin != null) {
            //更新
            userLogin.setUserName(userLoginAddRequest.getUserName());
            userLogin.setPassword(userLoginAddRequest.getPassword());
            userLogin.setUserId(userLoginAddRequest.getUserId());
            userLogin.setLoginType(userLoginAddRequest.getLoginType());
            userLogin.setOpenId(userLoginAddRequest.getOpenId());
            userLogin.setBizType(userLoginAddRequest.getBizType());
            userLoginService.updateUserLogin(userLogin);
        } else {
            //新增
            UserLogin addUserLogin = new UserLogin();
            addUserLogin.setUserName(userLoginAddRequest.getUserName());
            addUserLogin.setPassword(userLoginAddRequest.getPassword());
            addUserLogin.setUserId(userLoginAddRequest.getUserId());
            addUserLogin.setLoginType(userLoginAddRequest.getLoginType());
            addUserLogin.setOpenId(userLoginAddRequest.getOpenId());
            addUserLogin.setBizType(userLoginAddRequest.getBizType());
            userLoginService.addUserLogin(addUserLogin);
        }


    }

    /**
     * 获取用户登录信息
     */
    public UserLoginResp getUserLogin(String openId) {

        if (StringUtils.isEmpty(openId)) {
            throw new BizException(Meta.CODE_451.getCode(), "登陆标识id不合法");
        }
        UserLoginResp userLoginResp = new UserLoginResp();
        UserLogin userLogin = userLoginService.getUserLogin(openId);
        if (userLogin == null) {
            throw new BizException(Meta.CODE_451.getCode(), "该用户登陆信息不存在!");
        }
        userLoginResp.setUserName(userLogin.getUserName());
        userLoginResp.setPassword(userLogin.getPassword());
        userLoginResp.setUserId(userLogin.getUserId());
        userLoginResp.setLoginType(userLogin.getLoginType());
        userLoginResp.setOpenId(userLogin.getOpenId());
        userLoginResp.setBizType(userLogin.getBizType());
        userLoginResp.setCreateTime(userLogin.getCreateTime());
        userLoginResp.setLastModifiedTime(userLogin.getLastModifiedTime());
        return userLoginResp;

    }
}
